#endcoding:utf-8
class UsersController < ApplicationController
  layout "user_center" ,except: :new
  def new
    session.delete('user_id')
    @member = User.new
  end
  def mydemand
    status = params[:stat].nil? ? 1:params[:stat]
    if params[:stat].nil?
      @demands = Demand.where("user_id="+params[:id]).order("created_at desc").paginate :page=> params[:page],:per_page=> 12
    else
    @demands = Demand.where("user_id="+params[:id]+" AND status="+status.to_s).order("created_at desc").paginate :page=> params[:page],:per_page=> 12
    end

  end
  def mymessages
    isreaded = params[:readed].nil?||params[:readed].to_i==0 ? "0" : "1"
    if params[:readed].nil?
      @messages = User.find(session[:user_id]).messages.order("created_at desc").paginate :page=> params[:page],:per_page=> 10
    else
      @messages = User.find(session[:user_id]).messages.where("readed="+isreaded).order("created_at desc").paginate :page=> params[:page],:per_page=> 10
    end

  end
  def myquestions
    status = params[:stat].nil? ? 1:params[:stat]
    if params[:stat].nil?
      @questions = Question.where("user_id="+params[:id]).order("fixed_at desc,created_at desc").paginate :page=> params[:page],:per_page=> 10
    else
      @questions = Question.where("user_id="+params[:id]+" And fixed="+status.to_s).order("fixed_at desc,created_at desc").paginate :page=> params[:page],:per_page=> 10
    end
  end
  def myanswers
    status = params[:stat].nil? ? 1:params[:stat]
    if params[:stat].nil?
      @answers = Answer.where("user_id="+params[:id]).order("adopted_at desc,created_at desc").paginate :page=> params[:page],:per_page=> 10
    else
      @answers = Answer.where("user_id="+params[:id]+" And adopted="+status.to_s).order("adopted_at desc,created_at desc").paginate :page=> params[:page],:per_page=> 10
    end

  end
  def myfollowers

  end
  def myfollowing

  end
  def editprofile
    if isfactory?(userinfo)
      @info = FactoryInfo.find_by(:user_id=>uid)
      if !@info
        @info = FactoryInfo.new
        render "factory_infos/new"
      else
        render "factory_infos/edit"
      end
    else
      @info = CounselorInfo.find_by(:user_id=>uid)
      if !@info
        @info = CounselorInfo.new
        render "counselor_infos/new"
      else
        render "counselor_infos/edit"
      end
    end
  end
  def changeavatar
    @profile = Profile.find_by_user_id(uid)
    if !@profile
      @profile = Profile.new
    end
  end
  def updateavatar
    @profile = Profile.find_by_user_id(uid)
    if !@profile
      @profile = Profile.new(profile_params)
      @profile.save
    else
      @profile.update(profile_params)
    end
    redirect_to '/changeavatar'
  end
  def myoffers
    status = params[:status].nil? ? 0:params[:status];
    if params[:stat].nil?
      @offers = Offer.includes(:demand).where('user_id='+params[:id]).paginate :page=> params[:page],:per_page=> 12
    else
      @offers = Offer.includes(:demand).where('user_id='+params[:id]+' AND status='+status.to_s).paginate :page=> params[:page],:per_page=> 12
    end

  end
  def create
    @member = User.new(user_params)
    if @member.save
      @link = edit_account_activation_url(@member.activation_token, email: @member.email)
      UserMailer.account_activation(@member,"account_activation",@link).deliver
      flash[:info] = "请登录您的邮箱激活您的账号"
      redirect_to root_url
    else
      flash[:danger] = @member.errors.full_messages[0]
      redirect_to register_path
    end
  end

  def show
    @user = User.find(params[:id])
    # render :json=>@user
    @info = isfactory?(@user) ? FactoryInfo.find_by(:user_id=>params[:id]) : CounselorInfo.find_by(:user_id=>params[:id])
  end

  def follow
    @user = userinfo
    @other = User.find(params[:follow][:followed_id])
    if @user.follow(@other)
      render :json=>{:stat=>1}
    end
  end
  def unfollow
    @user = userinfo
    @other = User.find(params[:follow][:followed_id])
    if @user.unfollow(@other)
      render :json=>{:stat=>1}
    end
  end
  def card
    @user = User.find(params[:id])
    @info = getcardinfo(@user)
    @evaluates = Evaluate.select('users.name,evaluates.*').joins('left join users on evaluates.evaluater_id=users.id where evaluates.evaluated_id='+params[:id].to_s+' order by evaluates.created_at desc').paginate :page=> params[:page],:per_page=> 12
    if @user.user_type
      @demands = @user.demands.order('created_at desc').paginate :page=> params[:page],:per_page=> 12
    end
  end
  def demands
    @user = userinfo
    @demands = @user.demands.where("status="+params[:status]).paginate :page=> params[:page],:per_page=> 2
  end
  private
  def user_params
    params.require(:user).permit(:name, :email, :password,:password_confirmation,:user_type,:avatar,:avatar_cache)
  end
  def profile_params
    params[:profile].store('user_id',session[:user_id])
    params.require(:profile).permit(:avatar,:avatar_cache,:user_id)
  end
end
